<script setup>

import { reactive, toRefs, onMounted } from 'vue'
import api from '@/api'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/modules/user'

const user = useUserStore()
user.loadList()
    
const data = reactive({
    drawer: false,
    formSearch: {
        id: '',
        name: '',
        pym: ''
    },
    tableData: [],
    formData: {
        state: '',
        id: '',
        name: '',
        pym: ''
    }
})

const onSubmit = () => {
    return new Promise((resolve, reject) => {
        api.post('api/comp/classList', {
            pym: data.formSearch.pym
        }).then(res => {

            data.tableData = res.d
            
            resolve()
        })
    })
        
}

const submit = () => {
        
    switch (Number.parseInt(data.formData.state)) {
        case 0:
            api.post('api/comp/classEdit', {
                name: data.formData.name
            })
                .then(res => {
                    if (res.c === 0) {
                        ElMessage({
                            message: '添加成功',
                            type: ''
                        })
                    } else {
                        ElMessage({
                            message: res.m,
                            type: 'error'
                        })
                    }
                })
            break 
        case 1:
            api.post('api/comp/classEdit', {
                id: data.formData.id,
                name: data.formData.name
            })
                .then(res => {
                    if (res.c === 0) {
                        ElMessage({
                            message: '修改成功',
                            type: ''
                        })
                    } else {
                        ElMessage({
                            message: res.m,
                            type: 'error'
                        })
                    }
                })
            break
        case 2:
            api.post('api/comp/classDel', {
                id: data.formData.id
            })
                .then(res => {
                    if (res.c === 0) {
                        ElMessage({
                            message: '删除成功',
                            type: ''
                        })
                    } else {
                        ElMessage({
                            message: res.m,
                            type: 'error'
                        })
                    }
                })
            break
        default:
            break
    }
}

onSubmit()

const { formSearch, tableData, formData, drawer } = toRefs(data)

</script>

<template>
    <div class="content">
        <page-main>
            <div id="form">
                <div>
                    <el-form :inline="true" :model="formSearch" class="demo-form-inline" size="mini">
                        <el-form-item label="设备分类">
                            <el-input  v-model="formSearch.pym" style="width: 80px" placeholder="输入拼音码" />
                        </el-form-item>
                        
                        <el-form-item>
                            <el-button type="primary" @click="onSubmit">查询</el-button>
                        </el-form-item>
                    </el-form>
                </div>

                <el-table
                    :data="tableData" :cell-style="rowClass"
                    style="width: 330px;height:350px;font-size: 10px;"
                >
                    <el-table-column
                        fixed
                        prop="id"
                        label="ID"
                        width="80px"
                    />
                    <el-table-column
                        fixed
                        prop="name"
                        label="设备分类"
                        width="120px"
                    />
                    <el-table-column
                        fixed
                        prop="pym"
                        label="拼音码"
                        width="120px"
                    />
                </el-table>

                <!-- <el-button type="primary" style="margin-top: 20px;" @click="drawer = true">设备分类操作</el-button> -->

                <!-- <el-drawer
                    v-model="drawer"   
                    title="操作"			
                    direction="rtl"
                    custom-class="demo-drawer"
                    size="30%"
                > -->
                <el-card style="margin-top: 40px;">
                    <el-form>
                        <el-form-item label="操&nbsp&nbsp&nbsp作">
                            <el-select v-model="formData.state" placeholder="请选择操作">
                                <el-option label="增加" value="0" />
                                <el-option label="修改" value="1" />
                                <el-option label="删除" value="2" />
                            </el-select>
                        </el-form-item>
                        <div v-if="data.formData.state === '0'">
                            <el-form-item label="名&nbsp&nbsp&nbsp称">
                                <el-input  v-model="formData.name" />
                            </el-form-item>
                        </div>

                        <div v-if="data.formData.state === '1'">
                            <el-form-item label="ID">
                                <el-input  v-model="data.formData.id" />
                            </el-form-item>

                            <el-form-item label="名&nbsp&nbsp&nbsp称">
                                <el-input  v-model="formData.name" />
                            </el-form-item>
                        </div>

                        <div v-if="data.formData.state === '2'">
                            <el-form-item label="删除ID">
                                <el-input  v-model="data.formData.id" />
                            </el-form-item>
                        </div>

                        <el-form-item>
                            <el-button type="primary" :loading="loading" @click="submit">提&nbsp;&nbsp;&nbsp;&nbsp;交</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
                <!-- </el-drawer> -->
            </div>
        </page-main>
    </div>
</template>

<style>
   
</style>